﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Presentation_Layer.Properties;
using Presentation_Layer.WcfFlightRoutingService;

namespace Presentation_Layer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class SearchFlight : Window
    {
        private WcfFlightRoutingService.IService wcfService;

        #region searchFlight
        public SearchFlight()
        {
            InitializeComponent();
            wcfService = new WcfFlightRoutingService.ServiceClient();
            
            this.ComboBoxUpdate();
            this.flytoComboBoxUpdate();
            //this.ComboBoxSaveAirportUpdate();
            //List<Airport> airpList = new List<Airport>();
            //airpList = wcfService.GetAdjAirportList(101).ToList();

            //int[] airportintList = wcfService.GetAdjIdAirportList(101);
            //foreach (int i in airportintList)
            //{
            //    MessageBox.Show(i.ToString());
            //}
            
            // WORKS
            //Airport[] airportObjList = wcfService.GetAdjAirportList(101);
            //foreach(Airport a in airportObjList)
            //{
            //    MessageBox.Show(a.ToString());
            //}


            List<Airport> airportArr = wcfService.GetAllAirportNames();
            //List<Flight> flightArr = wcfService.GetAllFlights(true);
            //foreach(Flight f in flightArr)
            //{
            //    MessageBox.Show(f.Idflight.ToString());
            //}
            
            Airport airportOslo = wcfService.GetSelectedAirport(101);
            Airport airportCopenhagen = wcfService.GetSelectedAirport(103);
            List<Airport> airportDijkstraList = wcfService.RunDijkstra(airportArr, airportOslo, airportCopenhagen);
            //List<Airport> bestPathToTarget = wcfService.BacktrackDijkstra(airportBillund, airportDijkstraList);
            
            //for (int i = bestPathToTarget.Count; i >= 0; --i )
            //{
            //    MessageBox.Show(bestPathToTarget[i].ToString());
            //}

        }
        #endregion

        #region DepartureAirport logic
        #region getAllAirportNames in comboBox1
        private void ComboBoxUpdate() 
        {
            List<Airport> airportArr = wcfService.GetAllAirportNames();
            foreach (Airport airport in airportArr)
            {
                comboBox1.Items.Add(airport.Name);
            }

            /*
            if (comboBox1.SelectedIndex = 0)
            {
                DO SOMETHING...
            }
             */
        }
        #endregion

        private int ComboBoxSaveAirportUpdate()
        {
            List<Airport> airportArr = wcfService.GetAllAirportNames();

            Airport airportObj = new Airport();
            for (int i = 0; i < comboBox1.Items.Count - 1; i++)
            {
                if (comboBox1.SelectedIndex == i)
                {
                    airportObj = airportArr.ElementAt(i);
                    airportObj = (Airport)comboBox1.Items.GetItemAt(i);
                    MessageBox.Show(airportObj.IdAirport.ToString());
                }
            }

        
            int idDepartureAirport = airportObj.IdAirport;
            return idDepartureAirport;
        }
        #endregion

        #region DestinationAirport logic
        private void flytoComboBoxUpdate() 
        {
            List<Airport> airportArr = wcfService.GetAllAirportNames();
            foreach (Airport airport in airportArr)
            {
                flytoComboBox.Items.Add(airport.Name);
            }
        }

        private int flytoComboBoxAirportUpdate()
        {
            List<Airport> airportArr = wcfService.GetAllAirportNames();

            Airport airportObj = new Airport();
            for (int i = 0; i < comboBox1.Items.Count - 1; i++)
            {
                if (flytoComboBox.SelectedIndex == i)
                {
                    airportObj = airportArr.ElementAt(i);
                    //airportObj = comboBox1.Items.GetItemAt(i);
                }
            }
            int idDestinationAirport = airportObj.IdAirport;

            return idDestinationAirport;
        }
        #endregion

        #region searchButton
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //Dette bliver kaldt ved klik på Search knappen
            if (comboBox1.SelectedItem != null)
            {
            }

        }
        #endregion

        #region comboBox1 event
        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

            #region selectedIndex implementation

            List<Airport> airportArr = wcfService.GetAllAirportNames();

            Airport airportObj = new Airport();
            for (int i = 0; i < comboBox1.Items.Count - 1; i++)
            {
                if (comboBox1.SelectedIndex == i)
                {
                    airportObj = airportArr.ElementAt(i);
                    //airportObj = (Airport)comboBox1.Items.GetItemAt(i);
                    MessageBox.Show(airportObj.IdAirport.ToString());
                }
            }


            int idDepartureAirport = airportObj.IdAirport;


            //try 
            //{
            //    Airport airportObj = new Airport();

            //    int selectedIndex = comboBox1.SelectedIndex;
            //    airportObj.Name = (string) comboBox1.SelectedItem;

            //    MessageBox.Show("Selected Item Text: " + airportObj.ToString() + "\n" +
            //        "Index: " + selectedIndex.ToString());
            //}
            //catch (InvalidCastException ex) { throw new InvalidCastException(ex.Message); }
           
            #endregion


            if (comboBox1.SelectedItem != null)
            {
                //Skriver det markerede item fra comboboxen
                //MessageBox.Show(comboBox1.SelectedItem.ToString());
            }
        }
        #endregion

        private void ComboBox_SelectionChanged_2(object sender, SelectionChangedEventArgs e)
        {
            if (flytoComboBox.SelectedItem != null)
            {
                //Skriver det markerede item fra comboboxen
                //MessageBox.Show(comboBox1.SelectedItem.ToString());
            }
        }

        private void btnListAllFlights_Click(object sender, RoutedEventArgs e)
        {
            ListViewImp listViewImpObj = new ListViewImp();
            listViewImpObj.Owner = this;

            listViewImpObj.Show();
        }
    }
}